package cn.lsh.mapper;

import cn.lsh.writable.Weather;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.StringTokenizer;

/**
 * 集成Mapper，泛型说明：
 * 第一个	Mapper的key输入类型，map输入key就是行的字节位置，设置为LongWritable
 * 第二个	Mapper的value输入，就是行的内容，设置为Text
 * 第三个	Mapper的key输出类型，也是reduce的key输入类型，这里是设置的自定义类型
 * 第四个	Mapper的value输出类，也是reducer的value输入类型
 */
public class WeatherMapper extends Mapper<LongWritable, Text, Weather, IntWritable> {
	private final Weather w = new Weather();

	private final IntWritable output = new IntWritable();

	@Override
	protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
		//value格式：1949-10-01 14:21:02	34c
		StringTokenizer st = new StringTokenizer(value.toString());
		LocalDate dateTime = LocalDate.parse(st.nextToken(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		w.setYear(dateTime.getYear());
		w.setMonth(dateTime.getMonthValue());
		w.setDay(dateTime.getDayOfMonth());
		st.nextToken();
		String wds = st.nextToken();
		int wd = Integer.parseInt(wds.substring(0, wds.length() - 1));
		w.setWd(wd);
		output.set(wd);
		context.write(w, output);
	}
}
